@echo off
chcp 65001 >nul
echo 🚀 MongoDB Sync Setup and Test (Windows)
echo ======================================

REM 步骤1: 停止现有容器
echo [INFO] Step 1: Stopping existing containers...
docker-compose -f docker-compose-fixed.yml down -v

REM 步骤2: 启动数据库
echo [INFO] Step 2: Starting MongoDB databases...
docker-compose -f docker-compose-fixed.yml up -d mongodb-source mongodb-target

REM 步骤3: 等待数据库初始化
echo [INFO] Step 3: Waiting for database initialization...
timeout /t 45 /nobreak >nul

REM 步骤4: 检查数据库状态
echo [INFO] Step 4: Checking database status...
docker-compose -f docker-compose-fixed.yml ps

REM 步骤5: 运行诊断
echo [INFO] Step 5: Running diagnosis...
call diagnose-sync.bat

REM 步骤6: 执行同步
echo [INFO] Step 6: Starting sync process...
docker-compose -f docker-compose-fixed.yml --profile sync run --rm mongosync /home/mongosync/scripts/fixed-sync.sh

REM 步骤7: 验证结果
echo [INFO] Step 7: Final verification...
echo.
echo 📊 Final Status:
echo ================
echo Source Database (Port 20727):
echo   Connection: mongodb://admin:password1@localhost:20727/admin
echo   Users: 
docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()"
echo   Products: 
docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').products.countDocuments()"
echo   Orders: 
docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').orders.countDocuments()"

echo.
echo Target Database (Port 20728):
echo   Connection: mongodb://admin:password2@localhost:20728/admin
echo   Users: 
docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()"
echo   Products: 
docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').products.countDocuments()"
echo   Orders: 
docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').orders.countDocuments()"

echo.
echo [SUCCESS] Setup and sync completed!
echo.
echo [INFO] Useful commands:
echo   View logs: docker-compose -f docker-compose-fixed.yml logs
echo   Stop all: docker-compose -f docker-compose-fixed.yml down
echo   Connect to source: mongosh mongodb://admin:password1@localhost:20727/admin
echo   Connect to target: mongosh mongodb://admin:password2@localhost:20728/admin

pause 